#include <myhead.h>
#include "service.h"
#include "dao.h"
#include "mypkg.h"


int friend_service_list(char* username, char* sendbuf, int* bufLen){
	printf("friend_service_list: start. username=[%s]\n", username);
	db_user_t arr[100];
	int arrLen=100;
	//拼装返回报文
	char str[512]={0};
	if(-1 == tbl_friend_getList(username, 1, arr, &arrLen)){
		//拼装返回报文
		*bufLen=pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "ERROR", "查询tbl_friend_getList表时发生异常");
		return -1;
		
	}
	printf("list: 查询成功, arrLen=[%d]\n", arrLen);
	//拼装返回报文
	for(int i=0;i<arrLen;i++){
		printf("%s\n", arr[i].friendname);
		strcat(str, arr[i].friendname);
		strcat(str, "\n");
	}
	//
	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "OK", str);
	return 0;
}


int friend_service_add(char* username, char* friendname, char* sendbuf, int* bufLen){
	//拼装返回报文
	char str[512]={0};
	if(-1 == tbl_friend_insert(username, friendname, 1)){
		//拼装返回报文
		*bufLen=pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "ERROR", "查询tbl_friend_insert时发生异常");
		return -1;
	}
	//拼装返回报文
	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "OK", "好友添加成功");
	return 0;
}


int friend_service_delete(char* username, char* friendname, char* sendbuf, int* bufLen){
	/*
	//拼装返回报文
	pkg_pack(sendbuf, MSG_TYPE_RET, 2, "OK");
	char str[512]={0};
	if(-1 == tbl_friend_delete(username, friendname, 0)){
		//拼装返回报文
		*bufLen=pkg_pack(sendbuf, MSG_TYPE_RET, 2, "ERROR", "查询tbl_friend_delete时发生异常");
		return -1;
	}
	//拼装返回报文
	*/
	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "OK", "删除功能暂不支持");
	return 0;
}

int friend_service_chat(char* username, char* friendname, char* msg, int msglen, char* sendbuf, int *bufLen){
	//判断用户是不是你的好友
	int state=0;
	if(1 != tbl_friend_getState(username, friendname, &state)){
		//拼装返回报文
		*bufLen=pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "ERROR", "Ta还不是您的好友");
		return -1;
	}

	//判断用户是否在线
	ol_data_t* data=ol_link_findByName(friendname);                                     
	if(data == NULL){
    	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2,"ERROR","消息发送失败，好友不在线");
    	return -1;
	}

	if((time(NULL) - data->hearttime)>ONLINE_SECOND){
    	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2,"ERROR","消息发送失败，好友客户端已经10秒没有收到心跳");
    	return -1;
	}

	//组装转发报文给用户
	service_sendMsg(friendname, msg, msglen);

	//回复报文
	*bufLen = pkg_pack_buf(sendbuf, MSG_TYPE_RET, 2, "OK", "消息发送成功");
	return 0;
}


/*
int friend_service_add(db_user_t* user, char* errmsg){
	//查询好友名是否存在
	char* username=user->username;
	char* friendname=user->passwd;//借助传递
	
	db_user_t data;
	int ret=tbl_user_select(friendname, &data);
	if(ret == -1){
		strcpy(errmsg, "执行tbl_user_select失败");
		return -1;
	}

	if(ret == 0){
		strcpy(errmsg, "您要添加的好友不存在");
		return -1;
	}

	//查询是否已经是好友或发起过申请
	int state=0;
	ret = tbl_friend_getState(username, friendname, &state);
	if(ret == -1){
		strcpy(errmsg, "执行tbl_friend_select失败");
		return -1;
	}
	//有申请记录
	if(ret == 1){
		if(state == 0){
			strcpy(errmsg, "Ta已经是您的好友了");
			return -1;
		}
		// 还需系统再次推送消息给对方
	}

	//还没有申请记录
	if(ret == 0){
		if(tbl_friend_insert(username, friendname, 1)<0){
			strcpy(errmsg, "执行tbl_friend_insert失败");
			return -1;
		}
	}

	//向该好友发送系统消息
	char sendBuf[108];
	pkg_packSystemMsg(sendBuf, "您有个1条新的好友申请");
	sendMsg(friendname, sendBuf, sizeof(sendBuf));

	//返回
	return 0;
}

int friend_service_getReqNum(db_user_t* userinfo, char* errmsg){
	char* myname=userinfo->username;
	int num=tbl_friend_getNum(userinfo->username, 1);
	if(num < 0){
		strcpy(errmsg, "查询好友申请数量时发生了异常");
		return -1;
	}
	memcpy(errmsg, &num, sizeof(int));
	printf("!!!!!!!!!!!!!!!查询到[%d]条好友申请.\n", num);
	return num;
}
*/
